//
//  BBActivity.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/26.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit


class BBActivity: NSObject {
    
    var userId:String?
    var dayId:String?
    var activityType:Int?
    var activityId:String?
    var status:Int?
    var deviceId:String?
    var activityImage:String?
    var activityLines:String?
    var descriptionInfo:String? //description是关键字
    var title:String?
    var distance:Int?
    var usedTime:Int?
    var speedAvg:Int?
    var speedMin:Int?
    var speedMax:Int?
    var createTime:Int?
    var updateTime:Int?
    
    class func loadData(dayId:String,finish:@escaping (_ models:[BBActivity]?,_ error:Error?)->()){
        let path = "day/activity_list.do"
        let params = ["token":BBUser.loadUser()?.token, "userId":BBUser.loadUser()?.userId,"dayId":dayId]
        print(params)
        AFNetWorkManager.shareNetworkTools().post(path, parameters: params, success: { (_, JSON) in
            let dict = JSON as![String:AnyObject]
            let dictStatus = dict["status"]as!NSNumber
            if(dictStatus == 0){
                let data = dict["data"]
                let models = dict2Model(list: data!["list"] as! [[String:AnyObject]])
                finish(models,nil)
            }else{
                finish(nil,nil)
            }
        }) { (_, error) in
            finish(nil,error)
        }
    }
    
    //将字段数组转换为模型数组
    class func dict2Model(list:[[String:AnyObject]])->[BBActivity]{
        var models = [BBActivity]()
        for dict in list{
            models.append(BBActivity(dict: dict))
        }
        return models
    }
    
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        //setValuesForKeys(dict)
        userId = (dict["userId"] as! String)
        dayId = (dict["dayId"] as! String)
        activityType = (dict["activityType"] as! Int)
        activityId = (dict["activityId"] as! String)
        status = (dict["status"] as!Int)
        deviceId = (dict["deviceId"] as! String)
        activityImage = (dict["activityImage"] as? String)
        activityLines = (dict["activityLines"] as? String)
        descriptionInfo = (dict["description"] as? String)
        title = (dict["title"] as? String)
        distance = (dict["distance"] as?Int)
        usedTime = (dict["usedTime"] as?Int)
        speedAvg = (dict["speedAvg"] as?Int)
        speedMax = (dict["speedMax"] as?Int)
        createTime = (dict["createTime"] as! Int)
        updateTime = (dict["updateTime"] as! Int)
    }
}
